package mylab.io.core.codec;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioDatagramChannel;
import mylab.io.base.model.Msg;
import mylab.io.core.context.CoreContext;
import mylab.io.core.event.EndpointFoundEvent;
import mylab.utils.common.guava.AsyncEventUtil;

public class InboundUdpHandler extends AbstractInboundHandler {

    public InboundUdpHandler(CoreContext<?, ?> context) {
        super(context);
    }

    @Override
    protected void onChannelRead(ChannelHandlerContext ctx, Msg msg) {
        super.onChannelRead(ctx, msg);

        if (ctx.channel() instanceof NioDatagramChannel && msg != null) {
            //udp，只有在收到报文时发现对端
            AsyncEventUtil.postEvent(new EndpointFoundEvent(ctx.channel(), msg.getFrom().getEndpoint()));
        }
    }
}
